<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 设备管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.hospitalId" placeholder="医院ID" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
<!--                <el-select v-model="query.serialStatus"  placeholder="运行状态" class="handle-select mr10" @keyup.enter.native="handleSearch">-->
<!--                    <el-option key="00" label="开始" value="00"></el-option>-->
<!--                    <el-option key="01" label="停止" value="01"></el-option>-->
<!--                </el-select>-->
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="el-button" icon="el-icon-delete" @click="handleReset">重置</el-button>
<!--                <div class="right-items" style="float: right;">-->
<!--                    <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-show="isAdd">新增</el-button>-->
<!--                </div>-->
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column prop="serialPortname" label="串口名" width="200" align="center"></el-table-column>
                <el-table-column prop="serialBaudrate" label="串口波特率" align="center"></el-table-column>
<!--                <el-table-column prop="serialStatusText" label="运行状态" align="center"></el-table-column>-->
                <el-table-column prop="serialIstTm" label="插入时间" align="center" sortable></el-table-column>
                <el-table-column prop="serialUpdTm" label="更新时间" align="center"></el-table-column>
                <el-table-column prop="hospitalId" label="医院ID" align="center"></el-table-column>
                <el-table-column prop="hospitalSystemId" label="机器编号" align="center"></el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="30%" :close-on-click-modal="false">
            <el-form ref="addForm" :rules="addFormRules" :model="addForm" label-width="120px">
                <el-form-item label="编号:" prop="code">
                    <el-input v-model="addForm.code"></el-input>
                </el-form-item>
                <el-form-item label="串口名:" prop="serialPortname">
                    <el-select v-model="addForm.serialPortname" placeholder="串口名">
                        <el-option v-for="serialPortname in serialPortnames" :label="serialPortname" :key="serialPortname" :value="serialPortname"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="串口波特率:" prop="serialBaudrate">
                    <el-input type="number"  v-model="addForm.serialBaudrate" @keyup.native="proving1"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeAdd">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%" :close-on-click-modal="false">
            <el-form ref="editForm" :rules="addFormRules" :model="editForm" label-width="120px">
                <el-form-item label="编号:" prop="code">
                    <el-input disabled v-model="editForm.code"></el-input>
                </el-form-item>
                <el-form-item label="串口名:" prop="serialPortname">
                    <el-select v-model="editForm.serialPortname" placeholder="串口名">
                        <el-option v-for="serialPortname in serialPortnames" :label="serialPortname" :key="serialPortname" :value="serialPortname"></el-option>
                    </el-select>
<!--                    <el-input  v-model="editForm.serialPortname"></el-input>-->
                </el-form-item>
                <el-form-item label="串口波特率:" prop="serialBaudrate">
                    <el-input type="number" v-model="editForm.serialBaudrate" @keyup.native="proving1"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeEdit">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import { comData,doAdd,selectOne,doDelete,doStart,doUpdate,doEnd,getAllSerial} from '../../../../api/stand/comManage/comManage';
    export default {
        name: 'user',
        data() {
            return {
                query: {
                    hospitalId: '',
                    serialStatus: '',
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                editVisible: false,
                addVisible: false,
                pageTotal: 0,
                addForm:{},
                editForm: {},
                isAdd:true,
                idx: -1,
                id: -1,
                serialPortnames:[],
                addFormRules: {
                    code: [
                        { required: true, message: '请输入串口ID', trigger: 'blur' }
                    ],
                    serialPortname: [
                        { required: true, message: '请输入串口名称', trigger: 'blur' }
                    ],
                    serialBaudrate: [
                        { required: true, message: '请输入串口波特率', trigger: 'blur' }
                    ],
                },
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 获取后台数据
            getData() {
                comData(this.query).then(res => {
                    if (res.success == true){
                        this.tableData = res.data;
                        if (this.tableData.length==1){
                            this.isAdd=false;
                        }
                        this.pageTotal = res.total;
                    } else {
                        this.$message.error("查询失败！");
                        this.tableData = [];
                        this.pageTotal = 0;
                    }
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 触发重置按钮
            handleReset() {
                this.query.hospitalId = "";
                this.query.serialStatus = "";
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        //后台进行删除操作
                            doDelete({"code":row.code}).then(res => {
                                if (res.data==1){
                                    this.$message.success('删除成功');
                                    this.getData();
                                }else {
                                    this.$message.error('删除失败');
                                    this.getData();
                                }
                        });
                    })
                    .catch(() => {
                        this.$message({
                            message:'已取消删除'
                        });
                    });
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 新增操作
            handleAdd() {
                getAllSerial().then(res => {
                    if (res.success==true){
                        this.serialPortnames=res.data;
                        this.addVisible = true;
                    }else{
                        this.$message.error('获取可用串口失败');
                    }
                })
            },
            /*新增*/
            saveAdd(){
                this.$refs['addForm'].validate((valid) => {
                    if (valid) {
                          selectOne({"code":this.addForm.code}).then(res => {
                            if (res.data!==undefined && res.data!==null && res.data!=='{}'){
                                this.$message.error('该串口Id已存在,保存失败');
                            }else{
                                //新增form表单提交到后台
                                doAdd(this.addForm).then(res => {
                                    if (res.data==1){
                                        this.addVisible=false;
                                        this.addForm={};
                                        this.getData();
                                        this.$message.success('保存成功');
                                    }else{
                                        this.$message.error('保存失败');
                                    }
                                });
                            }
                        });

                    }
                });
            },
            // 编辑操作
            handleEdit(index, row) {
                //如果该串口已启用，不可编辑
                if (row.serialStatus=="00"){//该串口已启用
                    this.$message.error('该串口正在启用，无法编辑；请停止之后重新操作');
                }else{
                    getAllSerial().then(res => {
                        if (res.success==true){
                            this.serialPortnames=res.data;
                            selectOne({"code":row.code}).then(res => {
                                if (res.success==true){
                                    this.editForm=res.data;
                                }
                            });
                            this.editVisible = true;
                        }else{
                            this.$message.error('获取可用串口失败');
                        }
                    })

                }
            },
            closeEdit(){//关闭编辑弹出框
                this.editVisible=false;
                this.editForm={};
                this.getData();
            },
            closeAdd(){//关闭新增弹出框
                this.addVisible=false;
                this.addForm={};
                this.getData();
            },
            // 保存编辑
            saveEdit() {
                doUpdate(this.editForm).then(res => {
                    if (res.data == 1) {
                        this.editVisible = false;
                        this.editForm = {};
                        this.getData();
                        this.$message.success('编辑成功');
                    } else {
                        this.$message.error('编辑失败');
                    }
                })
            },
            //启用
            handleStart(index, row){
                doStart({"code":row.code}).then(res => {
                    if (res.data == 1) {
                        this.$message.success('启动成功');
                        this.getData();
                    } else {
                        this.$message.error('启动失败');
                        this.getData();
                    }
                });
            },
            //停用doEnd
            handleEnd(index, row){
                doEnd({"code":row.code}).then(res => {
                    if (res.success == true) {
                        this.$message.success('停用成功');
                        this.getData();
                    } else {
                        this.$message.error('停用失败');
                    }
                });
            },
            /*表单重置*/
            resetForm(refname){
                if (this.$refs[refname] !== undefined){
                    this.$refs[refname].resetFields();
                }
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
            proving1(e) {
                var keynum = window.event ? e.keyCode : e.which;   //获取键盘码
                var keychar = String.fromCharCode(keynum);  //获取键盘码对应的字符
                if (keynum ==189|| keynum==190||keynum == 109 || keynum == 110) {
                    this.$message.warning('禁止输入小数以及负数');
                    e.target.value = ' ';
                    return
                }
                if (keynum==69){
                    this.$message.warning('禁止输入e或E');
                    e.target.value = ' ';
                    return;
                }
            },
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
        /*border-radius 属性允许您为元素添加圆角边框！*/
        border-radius:6px;
        background-clip:padding-box;
        border-top-left-radius: 6px;
        border-top-right-radius: 6px;
        border-bottom-right-radius: 6px;
        border-bottom-left-radius: 6px;
    }
    /*头像上传*/
    .user-header{
        position: relative;
        display: inline-block;
    }
    .user-header-com{
        width: 100px;
        height: 100px;
        border-radius: 50%;
        display: inline-block;
    }
    .header-upload-btn{
        position: absolute;
        left: 0;
        top: 0;
        opacity: 0;
        /* 通过定位把input放在img标签上面，通过不透明度隐藏 */
    }
</style>
